package com.ingen.chakhleh.core.impl;

import java.util.Collection;


import com.googlecode.objectify.Key;
import com.ingen.chakhleh.core.api.dao.CourseDAO;
import com.ingen.chakhleh.core.api.dao.CurrencyDAO;
import com.ingen.chakhleh.core.api.dao.CusineDAO;
import com.ingen.chakhleh.core.api.dao.DishDAO;
import com.ingen.chakhleh.core.api.dao.DishRatingDAO;
import com.ingen.chakhleh.core.api.dao.PriceInfoDAO;
import com.ingen.chakhleh.core.api.dao.RatingTypeDAO;
import com.ingen.chakhleh.core.api.dao.RestaurantDAO;
import com.ingen.chakhleh.core.api.domain.Course;
import com.ingen.chakhleh.core.api.domain.Cusine;
import com.ingen.chakhleh.core.api.domain.Dish;
import com.ingen.chakhleh.core.api.domain.Restaurant;
import com.ingen.chakhleh.core.api.service.admin.ApplicationAdminService;

public class ApplicationAdminServiceImpl implements ApplicationAdminService{
	private CourseDAO courseDAO;
	private CurrencyDAO currencyDAO;
	private CusineDAO cusineDAO;
	private DishDAO dishDAO;
	private DishRatingDAO dishRatingDAO;
	private PriceInfoDAO priceInfoDAO;
	private RatingTypeDAO ratingTypeDAO;
	private RestaurantDAO restaurantDAO;

	
	public Course createCourse(Course course){
		Key<Course> courseKey = courseDAO.put(course);
		return courseDAO.get(courseKey);
	}
	
	public void createCourses(Collection<Course> courses){
		courseDAO.putAll(courses);
	}

	
	
	public Cusine createCusine(Cusine cusine){
		Key<Cusine> cusineKey = cusineDAO.put(cusine);
		return cusineDAO.get(cusineKey);
	}
	
	public void createCusines(Collection<Cusine> cusines){
		cusineDAO.putAll(cusines);
	}
	

	
	
	

	
	public Restaurant ceateRestaurant(Restaurant restaurant) {
		Key<Restaurant> restaurantKey = restaurantDAO.put(restaurant);
		return restaurantDAO.get(restaurantKey);
	}
	
	public void createRestaurants(Collection<Restaurant> restaurants) {
		restaurantDAO.putAll(restaurants);
	}
	
	public Dish createDish(Dish dish) {
		Key<Dish> dishKey = dishDAO.put(dish);
		return dishDAO.get(dishKey);
	}
	
	public void createDishes(Collection<Dish> dishes) {
		dishDAO.putAll(dishes);
	}
	
	

	//For dependency injection
	public void setCourseDAO(CourseDAO courseDAO) {
		this.courseDAO = courseDAO;
	}


	public void setCurrencyDAO(CurrencyDAO currencyDAO) {
		this.currencyDAO = currencyDAO;
	}


	public void setCusineDAO(CusineDAO cusineDAO) {
		this.cusineDAO = cusineDAO;
	}


	public void setDishDAO(DishDAO dishDAO) {
		this.dishDAO = dishDAO;
	}


	public void setDishRatingDAO(DishRatingDAO dishRatingDAO) {
		this.dishRatingDAO = dishRatingDAO;
	}


	public void setPriceInfoDAO(PriceInfoDAO priceInfoDAO) {
		this.priceInfoDAO = priceInfoDAO;
	}


	public void setRatingTypeDAO(RatingTypeDAO ratingTypeDAO) {
		this.ratingTypeDAO = ratingTypeDAO;
	}


	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}
	
	public Boolean addDishToRestaurant(Restaurant restaurant, Dish dish){
		if(restaurant == null || dish ==null){
			throw new IllegalArgumentException("Restaurant or Dish cannot be null");
		}
		
		if(restaurant.getId()==null){
			throw new IllegalArgumentException("Restaurant cannot have a null id property.");
		}
		
		Dish alreadyExistingDish = dishDAO.get(dish.getName());
		if(alreadyExistingDish==null){
			Key<Dish> dishKey = dishDAO.put(dish);
			alreadyExistingDish = dishDAO.get(dishKey);
		}
		
		Restaurant alreadyExistingRestaurant = restaurantDAO.get(restaurant.getId());;
		
		/*if(restaurant.getId()!=null){
			alreadyExistingRestaurant = restaurantDAO.get(restaurant.getId());
			if(alreadyExistingRestaurant!=null){
				if(alreadyExistingRestaurant.getDishes().contains(new Key<Dish>(Dish.class, alreadyExistingDish.getName()))){
					return false;
				}
			}else{
				Key<Restaurant> restaurantKey = restaurantDAO.put(restaurant);
				alreadyExistingRestaurant = restaurantDAO.get(restaurantKey);
			}
		}else{
			Key<Restaurant> restaurantKey = restaurantDAO.put(restaurant);
			alreadyExistingRestaurant = restaurantDAO.get(restaurantKey);
		}*/
		
		alreadyExistingRestaurant.getDishKeys().add(new Key<Dish>(Dish.class, alreadyExistingDish.getName()));
		restaurantDAO.put(alreadyExistingRestaurant);
		return true;
	}
	
	
	
	
}
